﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class LogUtil
    {
        private static readonly string dirName = "Log";
        private static readonly string dateFormat = "yyyy-MM-dd";
        private static object syncObj = new object();
        private static readonly string dateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
        /// <summary>
        /// 记录日志信息   
        /// </summary>
        /// <param name="content">日志信息</param>
        public static void Log(string content, string folder = "")
        {
            lock (syncObj)
            {
                try
                {
                    string path = Path.Combine(GetAppPath());
                    if (!string.IsNullOrEmpty(folder))
                        path = Path.Combine(path, folder);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    path = Path.Combine(path, string.Format("{0}.txt", DateTime.Now.ToString(dateFormat)));

                    StringBuilder sb = new StringBuilder(512);

                    sb.Append(DateTime.Now.ToString(dateTimeFormat));
                    sb.Append("=>");
                    sb.AppendLine(content);

                    File.AppendAllText(path, sb.ToString());
                }
                catch (Exception e)
                {
                }
            }
        }
        /// <summary>
        /// 当前应用路径
        /// </summary>
        /// <returns></returns>
        private static string GetAppPath()
        {
            if (!Directory.Exists("c:"))
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dirName);
            return Path.Combine("C:/Temp/MMP/", DateTime.Now.ToString(dateFormat), "FAS");
        }
    }
}
